<template>
  <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="dialogVisible" width="20%">
    <el-form ref="formItem" :model="formItem" label-width="80px" size="small" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="选择客户" prop="custId">
            <el-select v-model="formItem.custId" clearable filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteCustMethod">
              <el-option v-for="item in custOptions" :key="item.id" :label="item.name" :value="item.custId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="选择产品" prop="prodId">
            <el-select v-model="formItem.prodId" clearable filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteProdMethod">
              <el-option v-for="item in prodOptions" :key="item.id" :label="`${item.prodName}-${item.model}`" :value="item.prodId">
                <span style="float: left">{{ item.prodName }}-{{item.model}}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">￥{{ item.unitPrice }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="价格" prop="price">
            <el-input v-model="formItem.price" style="width: 215px"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="生效时间" prop="startDate">
            <el-date-picker v-model="formItem.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="失效时间" prop="endDate">
            <el-date-picker v-model="formItem.endDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleSubmit('formItem')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addPrice, updatePrice } from '@/api/price'
import { getTenCust } from '@/api/cust'
import { getTenProd } from '@/api/prod'
export default {
  data () {
    return {
      isEdit: false,
      dialogVisible: false,
      custOptions: [],
      prodOptions: [],
      formItem: {
        id: '',
        custId: '',
        prodId: '',
        price: '',
        startDate: '',
        endDate: ''
      },
      rules: {
        custId: [{ required: true, message: '请选择客户', trigger: 'blur' }],
        prodId: [{ required: true, message: '请选择产品', trigger: 'blur' }],
        price: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (row) {
      if (row) {
        this.title = '编辑'
        this.formItem = _.clone(row)
        this.isEdit = true
        delete this.formItem.updateTime
        if (row.custId) {
          this.custOptions = [{ custId: row.custId, name: row.custName }]
        } else {
          this.custOptions = []
        }
        if (row.prodId) {
          this.prodOptions = [{ prodId: row.prodId, prodName: row.prodName }]
        } else {
          this.prodOptions = []
        }
      } else {
        this.title = '新增'
        this.formItem = {
          id: '',
          custId: '',
          prodId: '',
          price: '',
          startDate: '',
          endDate: ''
        }
        this.isEdit = false
        this.custOptions = []
        this.prodOptions = []
      }
      this.dialogVisible = true
    },
    async remoteCustMethod (keyword) {
      if (keyword) {
        const res = await getTenCust({ keyword })
        if (res.code === 200) {
          this.custOptions = res.data
        } else {
          this.custOptions = []
        }
      } else {
        this.custOptions = [];
      }
    },
    async remoteProdMethod (keyword) {
      if (keyword) {
        const res = await getTenProd({ keyword })
        if (res.code === 200) {
          this.prodOptions = res.data
        } else {
          this.prodOptions = []
        }
      } else {
        this.prodOptions = [];
      }
    },
    handleSubmit (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const res = this.isEdit ? await updatePrice(this.formItem) : await addPrice(this.formItem)
          if (res.code === 200) {
            this.$emit('ok')
            this.dialogVisible = false
          } else {
            this.$message.error(res.msg)
          }
        }
      })
    }
  }
}
</script>

<style>
</style>